package com.woniuxy.service.impl;
/*
 * @author 幸琦林&&曾令忠
 *
 */
import java.util.ArrayList;
import java.util.List;

import com.woniuxy.dao.CoachInfoDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.dao.SimpleUserDao;
import com.woniuxy.entity.User;
import com.woniuxy.entity.Wallet;
import com.woniuxy.service.UserService;
import com.woniuxy.service.WalletService;
import com.woniuxy.util.CommonUtil;
import com.woniuxy.entity.CoachInfo;
import com.woniuxy.entity.SysAuthority;

import javax.annotation.Resource;


@Service
public class UserServiceImpl implements UserService {

	@Resource
	SimpleUserDao simpleUserDao;
	@Resource
	WalletService walletService;
	@Resource
	CoachInfoDao coachInfoDao;

	@Override
	public User userLogin(String account) {
		return simpleUserDao.loginUser(account);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int userRegister(User user) {
		int res = simpleUserDao.registerUser(user);
		System.out.println(user.getId());
		walletService.addWallet(user.getId());
		return res;
	}

    @Override
    public User findUserById(int userId) {
        return simpleUserDao.findUserById(userId);
    }

    /**
	 * @author 曾令忠
	 * 获取指定范围内的指定条件的用户
	 * distance:指定范围；User:查询条件的封装对象
	 */
	@Override
	public List<User> getUsersInDefinedDistance(double distance, User user) {
		//1经度/纬度对应111km
		user.setMinLatitude(user.getLatitude()-distance/111);
		user.setMaxLatitude(user.getLatitude()+distance/111);
		user.setMinLongitude(user.getLongitude()-distance/111);
		user.setMaxLongitude(user.getLongitude()+distance/111);
		return simpleUserDao.getUsersInDefinedDistance(user);
	}

	/**
	 * @author 曾令忠
	 * @param user:当前用户；
	 * @param n：公里数      得到最近n个用户，user封装查询条件
	 */
	@Override
	public List<User> getNearUser(User user,int n) {
		List<User> users = new ArrayList<>();
		double initialDistance = 5;//初始距离5公里
		double distance = initialDistance;
		while (users.size()<n&&distance<50) {
			users = getUsersInDefinedDistance(distance,user);
			distance += 5;
		}
        //得出最近的两个；
		users = CommonUtil.getNearUser(users, user, n);
        return users;
    }
	
	/**
	 * @author 曾令忠
	 * 针对获取到的附近用户判断是不是教练，如果是设定价格
	 */
	@Override
	public List<User> getNearAllUser(User user){
		List<User> users = getUsersInDefinedDistance(15,user);
		if("2".equals(user.getRole())) {
			for (User user2 : users) {
				Double price = coachInfoDao.selectPrice(user2.getId());
				if (price == null) {
					user2.setPrice(0);
				} else {
					user2.setPrice(price);
				}
			}
		}
		return users;
	}

    @Override
    public List<SysAuthority> findUserPerms(Long roleId) {
        return simpleUserDao.findUserPerms(roleId);
    }


    /**
     * @param account:账号或手机号
     * @param type:1表示账号，2表示手机号
     * @return 返回true，已存在账号
     */
    @Override
    public boolean userExist(String account, int type) {
        int flag = 0;
        if (type > 1) {
            flag = simpleUserDao.phoneExists(account);
        } else {
            flag = simpleUserDao.userExist(account);
        }
        return flag != 0;
    }

	@Override
	public Wallet getMyWallet(long id) {
		Wallet w = simpleUserDao.getMyWallet(id);
		return w;
	}

	/**
	 * 修改用户密码
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updatePwd(long id, String pwd) {
		simpleUserDao.updatePwd(id,pwd);
		
	}

	/**
	 * @author 曾令忠
	 * 角色认证
	 */
	@Override
	@Transactional
	public void setRole(long id, String role) {
		//改变角色
		simpleUserDao.setRole(id, role);
		//认证
		simpleUserDao.addToUserRole(id, Integer.parseInt(role) + 1 + "");
		//如果是教练添加，添加coachinfo
		if ("2".equals(role)) {
			CoachInfo cf = new CoachInfo();
			cf.setAcceptCourse("1");
			cf.setUserId(id);
			cf.setPriceHour(0);
			simpleUserDao.addCoachInfo(cf);
		}


	}



}
